#include <util/delay.h>
#include <avr/io.h>
#include "inout.h"

//PD2 - PD7, PC2 - PC3 	-> in	
//PB0 - PB5, PC0 - PC1 -> out

void ioinit(void)
{	
	DDRB |= (1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3) | (1 << PB4) | (1 << PB5);
	DDRC |= (1 << PC0) | (1 << PC1);
}

bool getstate(uint8_t in)
{
	bool state = true;
	in += 2;
	
	if(in <= 7)
	{	
		if(PIND & (1 << in))
			state = false;		
	} 
	else
	{	
		if(PINC & (1 << (in - 6)))
			state = false;
	}	

	return state;
}

/*void toggle_s(uint8_t pin, uint8_t seconds)
{
	PORTB ^= (1 << pin);
	uint8_t i;
	for(i = 0; i < seconds; i++)
		_delay_ms(1000);
	PORTB ^= (1 << pin);		
}*/

void delay_s(uint8_t seconds)
{
	uint8_t i;
	for(i = 0; i < seconds; i++)
		_delay_ms(1000);
}

void setoutp(uint8_t out)
{	
	uint16_t delaytime;	

	if(getstate(out))
	{
		delaytime = *delay[0];
		if(out <= 5)
		{
			PORTB ^= (1 << out);
			delay_s(delaytime);
			PORTB ^= (1 << out);
		}
		else
		{	
			PORTC ^= (1 << (out - 6));	
			delay_s(delaytime);
			PORTC ^= (1 << (out - 6));
		}
	}
	else
	{
		delaytime = *delay[1];
		if(out <= 5)
		{
			PORTB ^= (1 << out);
			delay_s(delaytime);
			PORTB ^= (1 << out);
		}
		else
		{
			PORTC ^= (1 << (out - 6));
			delay_s(delaytime);
			PORTC ^= (1 << (out - 6));
		}
	}
}


